require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("./login.less");

// 引入字体图标的css文件
require("../../assets/fonts/iconfont.css");

// 引入axios
let axios = require("axios");

// 引入获取节点的手写插件
const dom = require("../../utils/dom.js");

window.addEventListener("DOMContentLoaded", function () {
	dom.get("button").addEventListener("click", function () {
		// 获取账号框的值
		const user = dom.get("#user").value;
		// 获取密码框的值
		const pwd = dom.get("#pwd").value;

		// 验证账号
		let reg = /^1[3456789]\d{9}$/;
		if (reg.test(user)) {
			dom.get(".error").textContent = "";
		} else {
			dom.get(".error").style.color = "#f00";
			dom.get(".error").textContent = "请输入正确的手机号";
			return;
		}

		// 验证密码
		if (pwd.length >= 6) {
			dom.get(".error").textContent = "";
		} else {
			dom.get(".error").style.color = "#f00";
			dom.get(".error").textContent = "请输入正确的的密码";
			return;
		}

		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/isExist", {
				account: user,
			})
			.then((res) => {
				console.log(res);
				if (res.data.errno !== 0) {
					axios
						.post("http://fitness.h5.itsource.cn:3701/api/user/login", {
							account: user,
							password: pwd,
						})
						.then((res) => {
							console.log(res);
							if (res.data.errno === 0) {
								location.href = "./index.html";
							} else {
								dom.get(".error").textContent = res.data.message;
							}
						});
				} else {
					dom.get(".error").textContent = "账号不存在,请前往注册";
				}
			});
	});
});
